﻿using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.DAL
{
    public class UserDAL : IUserDAL
    {
        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="logins"></param>
        /// <returns></returns>
        public int AddUser(TBLogins logins)
        {
            Model1 model = new Model1();
            model.TBLogins.Add(logins);
            int n=model.SaveChanges();
            if (n > 0)
            {
                TBUsers users = new TBUsers();
                users.Id = logins.Id;
                users.Sex = 0;
                users.CreationTime = DateTime.Now;
                model.TBUsers.Add(users);
                model.SaveChanges();
            }
            return n;
        }

        /// <summary>
        /// 判断用户名是否存在
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool ExistUserName(string userName)
        {
            Model1 model = new Model1();
            //var list=model.TBLogins.Where(a => a.UserName == userName).ToList();
            //if (list.Count > 0)
            //{
            //    return true;
            //}
            //else{
            //    return false;
            //}

            //TBLogins login= model.TBLogins.FirstOrDefault(a => a.UserName == userName);
            //if (login != null)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
            return model.TBLogins.Any(a => a.UserName == userName);
        }

        /// <summary>
        /// 判断手机号是否存在
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool ExistMobileNumber(string mobileNumber)
        {
            Model1 model = new Model1();
            return model.TBLogins.Any(a => a.MobileNumber == mobileNumber);
        }

        /// <summary>
        /// 通过用户名查找登录信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public TBLogins FindByUserName(string userName)
        {
            Model1 model = new Model1();
            return model.TBLogins.FirstOrDefault(a => a.UserName == userName);
        }

        /// <summary>
        /// 通过用户id查找用户详细信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public TBUsers FindByUserId(int userId)
        {
            Model1 model = new Model1();
            return model.TBUsers.Find(userId);
            //return model.TBUsers.FirstOrDefault(a => a.Id == userId);
        }

        /// <summary>
        /// 通过手机号查找用户详细信息
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public TBLogins FindByMobile(string mobile)
        {
            Model1 model = new Model1();
            TBLogins login = model.TBLogins.SingleOrDefault(a => a.MobileNumber == mobile);
            return login;
        }

        /// <summary>
        /// 更新用户信息(不包含头像)
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public TBUsers EditUser(TBUsers user)
        {
            // 查询数据库
            Model1 model = new Model1();
            TBUsers userEntity = model.TBUsers.SingleOrDefault(m => m.Id == user.Id);
            if (userEntity == null)
            {
                return null;
            }
            userEntity.NickName = user.NickName;
            userEntity.Sex = user.Sex;
            userEntity.Birthday = user.Birthday;
            userEntity.Address = user.Address;
            userEntity.Hobby = user.Hobby;
            // 更新到数据库
            model.Entry(userEntity).State = EntityState.Modified;
            model.SaveChanges();
            // 返回结果
            return userEntity;
        }

        /// <summary>
        /// 更新用户头像
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public TBUsers EditAvatar(TBUsers user)
        {
            // 查询数据库
            Model1 model = new Model1();
            TBUsers userEntity = model.TBUsers.SingleOrDefault(m => m.Id == user.Id);
            if (userEntity == null)
            {
                return null;
            }
            userEntity.Avatar = user.Avatar;
            model.Entry(userEntity).State = EntityState.Modified;
            model.SaveChanges();
            return userEntity;
        }
    }
}
